﻿using OnlineDigitalCameraWebsite.Class;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace OnlineDigitalCameraWebsite
{
    public partial class GioHang : System.Web.UI.Page
    {
        private Invoice _selectedInvoice = new Invoice();
        public Invoice SelectedInvoice
        {
            get
            {
                if (_selectedInvoice.InvoiceId == "")
                {
                    try
                    {
                        _selectedInvoice = (Invoice)Session["Invoice"];
                    }
                    catch { }
                }
                return _selectedInvoice;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request["EmptyCart"] != null && Request["EmptyCart"].ToString().ToLower() == "true")
                {
                    SelectedInvoice.EmptyCart();
                    Session["Invoice"] = SelectedInvoice;
                }
                if (Request["Remove"] != null)
                {
                    int productToRemove = int.Parse(Request["Remove"].ToString());
                    SelectedInvoice.RemoveProduct(productToRemove);
                    Session["Invoice"] = SelectedInvoice;
                }
                RefreshDataInRepeater();
            }
        }
        private void RefreshDataInRepeater()
        {

            Repeater1.DataSource = SelectedInvoice.InvoiceItems;
            Repeater1.DataBind();
            Repeater1.Visible = (SelectedInvoice.InvoiceItems.Count > 0);
            lblEmptyCartMessage.Visible = (this.Repeater1.Visible == false);

        }
        protected void txtQty_TextChanged(object sender, System.EventArgs e)
        {
            try
            {
                 
                for (int idx = 0; idx <= Repeater1.Items.Count; idx++)
                {
                    try
                    {
                        Label lblIID = (Label)Repeater1.Items[idx].FindControl("lblProductId");
                        TextBox qtyObj = (TextBox)Repeater1.Items[idx].FindControl("txtQty");

                        int quantity = 0;
                        if (qtyObj.Text.Trim() != "")
                        {
                            quantity = int.Parse(qtyObj.Text.Trim());
                            if(quantity >10)
                            {
                                ShowMessage("Đặt số lượng sản phẩm <= 10 nhé.");
                                return;
                            }
                        }
                        int productId = int.Parse(lblIID.Text);
                        SelectedInvoice.UpdateProductQuantity(productId, quantity);
                        Session["Invoice"] = SelectedInvoice;

                    }
                    catch { }

                }
                RefreshDataInRepeater();

            }
            catch { }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        protected void btnDatHang_Click(object sender, EventArgs e)
        {
            //if (Session["LoaiThanhVien"].ToString() == "2")
            //{
            //    Response.Redirect("/ThanhToan.aspx");
            //}
            //else
            //{
            //    ShowMessage("Admin không mua được hàng trên website. Vui lòng đăng nhập tài khoản khác.");
            //}
        }

        public void ShowMessage(string Content)
        {
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script language='javascript'>alert('" + Content + "');</script>");
        }
    }
}